package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 死信队列配置类
 *
 * @author Fxz
 * @version 1.0
 * @date 2022-02-20 21:46
 */
@Configuration
public class DeadLetterMqConfig {

	public static final String exchange_dead = "exchange.dead";

	public static final String routing_dead_1 = "routing.dead.1";

	public static final String routing_dead_2 = "routing.dead.2";

	public static final String queue_dead_1 = "queue.dead.1";

	public static final String queue_dead_2 = "queue.dead.2";

	@Bean
	public Queue queue1() {
		HashMap<String, Object> map = new HashMap<>();
		// 死信交换机
		map.put("x-dead-letter-exchange", exchange_dead);
		// 死信路由键
		map.put("x-dead-letter-routing-key", routing_dead_2);
		// 设置延迟时间
		map.put("x-message-ttl", 10 * 1000);
		return QueueBuilder.durable(queue_dead_1).withArguments(map).build();
	}

	@Bean
	public DirectExchange exchange() {
		return ExchangeBuilder.directExchange(exchange_dead).durable(true).build();
	}

	@Bean
	public Binding binding1() {
		return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
	}

	@Bean
	public Queue queue2() {
		return QueueBuilder.durable(queue_dead_2).build();
	}

	@Bean
	public Binding binding2() {
		return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
	}

}
